package com.eastfair.venueservice.service.impl;

import com.eastfair.builder.ExhibitorFeign;
import com.eastfair.builder.dto.ExhibitorDTO;
import com.eastfair.builder.enumeration.ExhibitorEarnestMoneyPayStatusEnum;
import com.eastfair.builder.vo.ExhibitorVO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.entity.BondManage;
import com.eastfair.venueservice.enums.BondManagePayStatusEnum;
import com.eastfair.venueservice.service.VenueManageExhibitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 展商服务.
 *
 * @author dqq
 * @date 2022 /12/12
 */
@Service
@Slf4j
public class VenueManageExhibitorServiceImpl implements VenueManageExhibitorService {

    @Resource
    private ExhibitorFeign exhibitorFeign;

    @Override
    public List<ExhibitorVO> queryExhibitors(ExhibitorDTO exhibitorDTO) {
        log.info("queryExhibitors - 查询展商VO列表, exhibitorDTO={}", exhibitorDTO);
        R<List<ExhibitorVO>> result = exhibitorFeign.queryExhibitors(exhibitorDTO);
        log.info("queryExhibitors - 查询展商VO列表, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public ExhibitorVO getExhibitor(Long id) {
        log.info("getExhibitor - 查询展商VO列表, id={}", id);
        R<ExhibitorVO> result = exhibitorFeign.getExhibitor(id);
        log.info("getExhibitor - 查询展商VO列表, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean updateShouldEarnestMoney(ExhibitorDTO exhibitorDTO) {
        log.info("updateShouldEarnestMoney - 更新展商应付保证金金额, exhibitorDTO={}", exhibitorDTO);
        R<Boolean> result = exhibitorFeign.updateShouldEarnestMoney(exhibitorDTO);
        log.info("updateShouldEarnestMoney - 更新展商应付保证金金额, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean updateEarnestMoneyPayStatus(ExhibitorDTO exhibitorDTO) {
        log.info("updateEarnestMoneyPayStatus - 更新保证金支付状态, exhibitorDTO={}", exhibitorDTO);
        R<Boolean> result = exhibitorFeign.updateEarnestMoneyPayStatus(exhibitorDTO);
        log.info("updateEarnestMoneyPayStatus - 更新保证金支付状态, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean updateEarnestMoneyByBondManage(BondManage bondManage) {
        ExhibitorDTO exhibitorDTO = new ExhibitorDTO();
        exhibitorDTO.setId(bondManage.getExhibitorId());
        exhibitorDTO.setActualEarnestMoney(bondManage.getPayAmount());
        if (BondManagePayStatusEnum.PARTPAY.getCode().equals(bondManage.getPayStatus())) {
            exhibitorDTO.setEarnestMoneyPayStatus(ExhibitorEarnestMoneyPayStatusEnum.PARTPAY);
        } else if (BondManagePayStatusEnum.PAYED.getCode().equals(bondManage.getPayStatus())) {
            exhibitorDTO.setEarnestMoneyPayStatus(ExhibitorEarnestMoneyPayStatusEnum.HAS_PAY);
        } else {
            exhibitorDTO.setEarnestMoneyPayStatus(ExhibitorEarnestMoneyPayStatusEnum.NOT_PAY);
        }
        return updateEarnestMoneyPayStatus(exhibitorDTO);
    }

    @Override
    public boolean updateRemainEarnestMoney(ExhibitorDTO exhibitorDTO) {
        log.info("updateRemainEarnestMoney - 更新保证金余额, exhibitorDTO={}", exhibitorDTO);
        R<Boolean> result = exhibitorFeign.updateRemainEarnestMoney(exhibitorDTO);
        log.info("updateRemainEarnestMoney - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }
}